{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Altair in `JupyterLite`\n",
    "\n",
    "**Altair** is a declarative statistical visualization library for Python.\n",
    "\n",
    "Most of the examples below are from: https://altair-viz.github.io/gallery"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import the dependencies:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install -q altair"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Simple Bar Chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import altair as alt\n",
    "import pandas as pd\n",
    "\n",
    "source = pd.DataFrame(\n",
    "    {\n",
    "        \"a\": [\"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\", \"I\"],\n",
    "        \"b\": [28, 55, 43, 91, 81, 53, 19, 87, 52],\n",
    "    }\n",
    ")\n",
    "\n",
    "alt.Chart(source).mark_bar().encode(x=\"a\", y=\"b\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Simple Heatmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import altair as alt\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# Compute x^2 + y^2 across a 2D grid\n",
    "x, y = np.meshgrid(range(-5, 5), range(-5, 5))\n",
    "z = x**2 + y**2\n",
    "\n",
    "# Convert this grid to columnar data expected by Altair\n",
    "source = pd.DataFrame({\"x\": x.ravel(), \"y\": y.ravel(), \"z\": z.ravel()})\n",
    "\n",
    "alt.Chart(source).mark_rect().encode(x=\"x:O\", y=\"y:O\", color=\"z:Q\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Install the Vega Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install -q vega-datasets"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Interactive Average"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import altair as alt\n",
    "from vega_datasets import data\n",
    "\n",
    "source = data.seattle_weather()\n",
    "brush = alt.selection_interval(encodings=[\"x\"])\n",
    "\n",
    "bars = (\n",
    "    alt.Chart()\n",
    "    .mark_bar()\n",
    "    .encode(\n",
    "        x=\"month(date):O\",\n",
    "        y=\"mean(precipitation):Q\",\n",
    "        opacity=alt.condition(brush, alt.OpacityValue(1), alt.OpacityValue(0.7)),\n",
    "    )\n",
    "    .add_params(brush)\n",
    ")\n",
    "\n",
    "line = (\n",
    "    alt.Chart()\n",
    "    .mark_rule(color=\"firebrick\")\n",
    "    .encode(y=\"mean(precipitation):Q\", size=alt.SizeValue(3))\n",
    "    .transform_filter(brush)\n",
    ")\n",
    "\n",
    "alt.layer(bars, line, data=source)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Locations of US Airports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import altair as alt\n",
    "from vega_datasets import data\n",
    "\n",
    "airports = data.airports.url\n",
    "states = alt.topo_feature(data.us_10m.url, feature=\"states\")\n",
    "\n",
    "# US states background\n",
    "background = (\n",
    "    alt.Chart(states)\n",
    "    .mark_geoshape(fill=\"lightgray\", stroke=\"white\")\n",
    "    .properties(width=500, height=300)\n",
    "    .project(\"albersUsa\")\n",
    ")\n",
    "\n",
    "# airport positions on background\n",
    "points = (\n",
    "    alt.Chart(airports)\n",
    "    .transform_aggregate(\n",
    "        latitude=\"mean(latitude)\",\n",
    "        longitude=\"mean(longitude)\",\n",
    "        count=\"count()\",\n",
    "        groupby=[\"state\"],\n",
    "    )\n",
    "    .mark_circle()\n",
    "    .encode(\n",
    "        longitude=\"longitude:Q\",\n",
    "        latitude=\"latitude:Q\",\n",
    "        size=alt.Size(\"count:Q\", title=\"Number of Airports\"),\n",
    "        color=alt.value(\"steelblue\"),\n",
    "        tooltip=[\"state:N\", \"count:Q\"],\n",
    "    )\n",
    "    .properties(title=\"Number of airports in US\")\n",
    ")\n",
    "\n",
    "background + points"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (Pyodide)",
   "language": "python",
   "name": "python"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "python",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
